/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Modulo1;

import Modelo.DAL.Estadistica_DAO;
import Modelo.EsImplicado;
import Modelo.ReporteNovedad;
import java.io.PrintWriter;
import java.util.Date;
import java.text.SimpleDateFormat;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.json.JSONArray;
import org.json.JSONObject;

/**
 *
 * @author dymanik
 */
public class consultarEstadisticasAction extends org.apache.struts.action.Action {
    /* forward name="success" path="" */

    private static final String SUCCESS = "success";
    private static final String FAILURE = "failure";

    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        String desde = request.getParameter("desde");
        String hasta = request.getParameter("hasta");
        String grupo = request.getParameter("grupo");
        String personalDsi = request.getParameter("personalDsi");
        String sede = request.getParameter("sede");
        String espacio = request.getParameter("espacio");
        String[] esp = request.getParameterValues("novedadesEspecificas");
        String[] gen = request.getParameterValues("novedadesGenericas");

        SimpleDateFormat formato = new SimpleDateFormat("dd/MM/yy");
        Date fechaIni = null;
        Date fechaFin = null;

        boolean error = false;
        ActionErrors errors = new ActionErrors();
        if (desde != null && !desde.equals("")) {
            fechaIni = formato.parse(desde);
            if (hasta != null && !hasta.equals("")) {
                fechaFin = formato.parse(hasta);
                if (!fechaIni.before(fechaFin)) {
                    errors.add("Est_FechaError", new ActionMessage("errors.Est_FechaError"));
                    saveErrors(request, errors);
                    error = true;
                }
            }
        }

        ReporteNovedad reporte = new ReporteNovedad();
        EsImplicado implicado = new EsImplicado();

        response.setContentType("text/json");
        JSONObject data = Estadistica_DAO.consultarEstadisticaRep(fechaIni, fechaFin, esp, personalDsi, sede, espacio);
        PrintWriter pw = response.getWriter();
        pw.write(data.toString());
        pw.flush();
        pw.close();

        return mapping.findForward(SUCCESS);
    }
}
